<?php
/*------------------------------------------------------------------------
 * People.php 
 *
 * 个人主页控制器
 * 	
 * Created on 2017-01-08
 *
 * Author: hwc123 <hwc123@139.com>
 * 
 * Copyright (c) 2017 All rights reserved.
 * ------------------------------------------------------------------------
 */

namespace app\index\controller;

class People extends Base{

	function _initialize($is_user_login = false){
		parent::_initialize($is_user_login);
		$user_id = $this->_get('user_id');
		$info = model('User')->user_info($user_id);
		$this->add_step($info['show_name']);
		if(!$info){
			$this->error('对不起，您的操作有误！');
		}
		$this->assign('info', $info);
		$this->assign('is_focus', model('User')->is_fav($this->_user_id, $user_id, 'user_focus'));
	}
	public function index(){
		$user_id = $this->_get('user_id', '对不起，您的操作有误！');
		$this->redirect(url('People/question', array('user_id' => $user_id)));
		
		return $this->view();
	}
	// 用户提问
	public function question(){
		$user_id = $this->_get('user_id', '对不起，您的操作有误！');
		if($user_id != $this->_user_id){
			$where['is_anonymous'] = 0;
		}
		$where['user_id']   = $user_id;
		$where['is_delete'] = 0;
		$where['status']    = 1;
		$order = 'order_id,ask_id desc';
		$list = model('Ask')->where($where)->order($order)->paginate(10, false);
		$this->assign('list', $list);
		$this->assign('page', $list->render());

		return $this->view();
	}
	// 用户回答
	public function answer(){
		$user_id = $this->_get('user_id', '对不起，您的操作有误！');

		$ask_id_arr = model('AskAnswer')->distinct(true)->where(array('user_id' => $user_id, 'is_delete' => 0))->column('ask_id');
		$ask_id_str = implode(',', $ask_id_arr);
		$ask_id_str = $ask_id_str ? $ask_id_str : 0;
		
		if($user_id != $this->_user_id){
			$where['is_anonymous'] = 0;
		}
		$where['ask_id']    = array('in', $ask_id_str);
		$where['is_delete'] = 0;
		$where['status']    = 1;
		$order = 'order_id,ask_id desc';
		$list = model('Ask')->where($where)->order($order)->paginate(10, false);
		$this->assign('list', $list);
		$this->assign('page', $list->render());

		return $this->view();
	}
	// 收藏问题
	public function fav_question(){
		$this->add_step('收藏的问题');
		$this->user_id(true);
		$ask_ids = model('User')->get_fav_object_ids($this->_user_id, 'ask_fav');
		$where['ask_id']    = array('in', $ask_ids);
		$where['is_delete'] = 0;
		$where['status']    = 1;
		$order = 'field(ask_id,' . $ask_ids . ')';
		$list = model('Ask')->where($where)->order($order)->paginate(10, false);
		$this->assign('list', $list);
		$this->assign('nav_is_fav', 1);
		$this->assign('page', $list->render());

		return $this->view();
	}
	// 收藏文章
	public function fav_article(){
		$this->add_step('收藏的文章');
		$this->user_id(true);
		$article_ids = model('User')->get_fav_object_ids($this->_user_id, 'article_fav');
		$where['article_id']    = array('in', $article_ids);
		$where['is_delete'] = 0;
		$where['status']    = 1;
		$order = 'field(article_id,' . $article_ids . ')';
		$list = model('Article')->where($where)->order($order)->paginate(10, false);
		$this->assign('list', $list);
		$this->assign('nav_is_fav', 1);
		$this->assign('page', $list->render());

		return $this->view();
	}
	// 关注问题
	public function focus_question(){
		$this->add_step('关注的问题');
		$this->user_id(true);
		$ask_ids = model('User')->get_fav_object_ids($this->_user_id, 'ask_focus');
		$where['ask_id']    = array('in', $ask_ids);
		$where['is_delete'] = 0;
		$where['status']    = 1;
		$order = 'field(ask_id,' . $ask_ids . ')';
		$list = model('Ask')->where($where)->order($order)->paginate(10, false);
		$this->assign('list', $list);
		$this->assign('nav_is_focus', 1);
		$this->assign('page', $list->render());

		return $this->view();
	}
	// 我关注的人
	public function focus_user(){
		$this->add_step('我关注的人');
		$this->user_id(true);
		$user_ids = model('User')->get_fav_object_ids($this->_user_id, 'user_focus');
		$where['user_id'] = array('in', $user_ids);
		$where['status']  = 1;
		$order = 'field(user_id,' . $user_ids . ')';
		$list  = model('User')->where($where)->order($order)->paginate(10, false);
		$this->assign('list', $list);
		$this->assign('nav_is_focus', 1);
		$this->assign('page', $list->render());

		return $this->view();
	}
	// 关注我的人
	public function focus_me(){
		$this->add_step('关注我的人');
		$this->user_id(true);
		$user_id_arr = db('UserFav')->where(array('fav_type' => 'user_focus', 'object_id' => $this->_user_id))->order('fav_id desc')->column('user_id');
		$user_id_str = implode(',', $user_id_arr);
		$user_id_str = $user_id_str ? $user_id_str : 0;

		$where['user_id'] = array('in', $user_id_str);
		$where['status']  = 1;
		$order = 'field(user_id,' . $user_id_str . ')';
		$list  = model('User')->where($where)->order($order)->paginate(10, false);
		$this->assign('list', $list);
		$this->assign('nav_is_focus', 1);
		$this->assign('page', $list->render());

		return $this->view();
	}
	// 消息
	public function message(){
		$this->add_step('消息');
		$this->user_id(true);
		$msg_type = $this->_post('msg_type', '', '');
		if($msg_type){
			$where['msg_type'] = $msg_type;
		}
		if($msg_type == 'to_me'){
			$where['msg_type'] = array('in', 'to_me_comment');
		}
		// 消息已读
		if(!$msg_type){
			model('Message')->setUserMessageReaded($this->_user_id);
		}

		$where['user_id'] = $this->_user_id;
		$order = 'log_id desc';
		$list  = model('MessageLog')->where($where)->order($order)->paginate(10, false);

		$this->assign('list', $list);
		$this->assign('nav_is_message', 1);
		$this->assign('page', $list->render());
		return $this->view();
	}
	// 关注用户
	public function set_focus(){
		// 设置需要登录才能访问
		$this->user_id(true);
		$user_id = $this->_get('user_id', '对不起，您的操作有误！');
		if($this->_user_id == $user_id){
			$this->error('对不起，不能关注自己！');
		}
		$model = model('User');
		$reid  = $model->set_fav('User', 'by_focus_user_num', 'user_id', $this->_user_id, $user_id, 'user_focus');
		
		$data['node_id'] = 'data-' . $user_id;
		$data['node_data']['by_focus_user_num'] = $model->_total_fav_num;
		if($reid === false){
			$tips_info = model('User')->tips_info;
		}
		if($reid == 1){
			$tips_info = '关注成功！';
			model('UserData')->update_user_data($this->_user_id, 'focus_user_num');
		}
		if($reid == 2){
			$tips_info = '取消关注成功！';
			model('UserData')->update_user_data($this->_user_id, 'focus_user_num', -1);
		}
		
		$this->success($tips_info, '', $data);
	}
}